from __init__ import OGMSService_DEBUG

# bind server A
s103 = OGMSService_DEBUG.CreateServer("172.21.212.103", 8060)
ac103 = s103.createServiceAccess()

# upload data
dateData = ac103.uploadDataByFile("Date", "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\Date.dat")
sitesData = ac103.uploadDataByFile("AirSites", "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\BeijingAirSites.zip")
msiteData = ac103.uploadDataByFile("MeteorologicalSite", "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\MeteorologicalSite.dat")
mfieldsData = ac103.uploadDataByFile("MeteorologicalFields", "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\MeteorologicalFields.dat")
mData = ac103.uploadDataByFile("MeteorologicalData", "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\MeteorologicalData.zip")
prjData = ac103.uploadDataByFile("PrjData", "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\Projection.prj")

# bind model service
svIDW_extract = ac103.getModelServiceByID('5eb19b154b1d6302ac0637ce')

# data configuration
dataColl = [
    ac103.createDataConfigurationItem("SitesDataBuilding", "Date", dateData),
    ac103.createDataConfigurationItem("SitesDataBuilding", "Airsites", sitesData),
    ac103.createDataConfigurationItem("SitesDataBuilding", "Meteorologicalsites", msiteData),
    ac103.createDataConfigurationItem("SitesDataBuilding", "Meteorologicalfieldsfile", mfieldsData),
    ac103.createDataConfigurationItem("SitesDataBuilding", "Meteorologicaldata", mData),
    ac103.createDataConfigurationItem("IDW", "PRJ", prjData)
]
# invoke
rcIDW_extract = svIDW_extract.invoke(dataColl)

rcIDW_extract = ac103.getModelServiceRunningRecordByID(rcIDW_extract)
rcIDW_extract.wait4Finished()

sitesdata = "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\SitesData.zip"
for index,item in enumerate(rcIDW_extract.outputs):
    if item.statename == "IDW" and item.eventname == "Sitesdatafull":
        dat = ac103.getDataByID(item.dataid)
        dat.save(sitesdata)

# bind server B
s30 = OGMSService_DEBUG.CreateServer("172.21.212.30", 8060)
ac30 = s30.createServiceAccess()

# upload data
airsites = ac30.uploadDataByFile("AirSites", "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\BeijingSites.csv")
trainingcount = ac30.uploadDataByFile("TrainingCount", "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\trainingSitesNum.txt")

# bind model service
svSitePicking = ac30.getModelServiceByID('5eb19b154b1d6302ac0637ce')

# data configuration
dataColl = [
    ac30.createDataConfigurationItem("SitePickingByRandom", "Sites", airsites),
    ac30.createDataConfigurationItem("SitePickingByRandom", "TrainingCount", trainingcount)
]

# invoke
rcSitePicking = svSitePicking.invoke(dataColl)

rcSitePicking = ac30.getModelServiceRunningRecordByID(rcSitePicking)
rcSitePicking.wait4Finished()

trainingsites = "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\Trainingsites.csv"
validationsites = "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\Trainingsites.csv"
for index,item in enumerate(rcSitePicking.outputs):
    if item.statename == "SitePickingByRandom" and item.eventname == "TrainingSites":
        dat = ac103.getDataByID(item.dataid)
        dat.save(trainingsites)
    if item.statename == "SitePickingByRandom" and item.eventname == "ValidationSites":
        dat = ac103.getDataByID(item.dataid)
        dat.save(validationsites)

# bind server C
s223 = OGMSService_DEBUG.CreateServer("172.21.212.223", 8060)
ac223 = s223.createServiceAccess()

# upload data
dateData = ac223.uploadDataByFile("Date", "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\Date.dat")
airData = ac223.uploadDataByFile("AirData", "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\AirData_2014.zip")
sitesData = ac223.uploadDataByFile("SitesData", sitesData)
trainingSitesData = ac223.uploadDataByFile("trainingSitesData", trainingsites)
validationSitesData = ac223.uploadDataByFile("validationSitesData", validationsites)
fieldsData = ac223.uploadDataByFile("Fields", "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\Fields.dat")

# bind model service
svPreprocess = ac223.getModelServiceByID('5eb19b154b1d6302ac0637ce')

# data configuration
dataColl = [
    ac223.createDataConfigurationItem("PM25DataMatching", "Date", dateData),
    ac223.createDataConfigurationItem("PM25DataMatching", "Airsites", airData),
    ac223.createDataConfigurationItem("PM25DataMatching", "SitesData", sitesData),
    ac223.createDataConfigurationItem("Preprocessing", "TrainingSites", trainingSitesData),
    ac223.createDataConfigurationItem("Preprocessing", "ValidationSites", validationSitesData),
    ac223.createDataConfigurationItem("Preprocessing", "Fields", fieldsData)
]

# invoke
rcPreprocess = svPreprocess.invoke(dataColl)

rcPreprocess = ac30.getModelServiceRunningRecordByID(rcPreprocess)
rcPreprocess.wait4Finished()

datTrainingSource = None
datTrainingTarget = None
datValidationPrediction = None
datValidationReal = None

for index,item in enumerate(rcPreprocess.outputs):
    if item.statename == "Preprocessing" and item.eventname == "TrainingSourceData":
        dat = ac223.getDataByID(item.dataid)
        datTrainingSource = dat
    if item.statename == "Preprocessing" and item.eventname == "TrainingTargetData":
        dat = ac223.getDataByID(item.dataid)
        datTrainingTarget = dat
    if item.statename == "Preprocessing" and item.eventname == "ValidationPredictedData":
        dat = ac223.getDataByID(item.dataid)
        datValidationPrediction = dat
    if item.statename == "Preprocessing" and item.eventname == "ValidationRealData":
        dat = ac223.getDataByID(item.dataid)
        datValidationReal = dat


# bind model service
svTraining = ac223.getModelServiceByID('5d02747adcdb950e849690ea')

# data configuration
dataColl = [
    ac223.createDataConfigurationItem("Training", "Source", datTrainingSource),
    ac223.createDataConfigurationItem("Training", "Target", datTrainingTarget)
]

# invoke
rcTraining = svTraining.invoke(dataColl)

rcTraining = ac30.getModelServiceRunningRecordByID(rcTraining)
rcTraining.wait4Finished()

predictingModel = None

for index,item in enumerate(rcPreprocess.outputs):
    if item.statename == "Training" and item.eventname == "Model":
        dat = ac223.getDataByID(item.dataid)
        predictingModel = dat


# bind model service
svPredicting = ac223.getModelServiceByID('5d0279eddcdb950e849690f3')

# data configuration
dataColl = [
    ac223.createDataConfigurationItem("Predicting", "Model", predictingModel),
    ac223.createDataConfigurationItem("Predicting", "PredictionData", datValidationPrediction),
    ac223.createDataConfigurationItem("Validation", "RealData", datValidationReal)
]

# invoke
rcPredicting = svPredicting.invoke(dataColl)

rcPredicting = ac30.getModelServiceRunningRecordByID(rcPredicting)
rcPredicting.wait4Finished()

result = "E:\\DemoData\\GeoModeling\\Air\\Package\\DemoData\\Result.csv"

for index,item in enumerate(rcPreprocess.outputs):
    if item.statename == "Validation" and item.eventname == "Result":
        dat = ac223.getDataByID(item.dataid)
        dat.save(result)

